<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/16
 * Time: 11:51
 */

namespace Admin\Controller;


class TeacherController extends AuthController{
    public function  index(){
        $teacher=M('teacher');
        $result = $teacher->join('dx_sex ON dx_teacher.teacher_sex = dx_sex.sex_p')->select();

       // dump($result);
        $this->assign('list',$result);
        $this->display();
    }
    public function  add(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =      './Public/'; // 设置附件上传根目录
        $upload->savePath  =      '/Uploads/'; // 设置附件上传（子）目录
        $info   =   $upload->upload();
        $array = array();
        if ($info) {
            foreach ($info as $val) {
                $array[] = $val['savepath'] . $val['savename'];

            }
        }
        $post = I('post.');
        $post['teacher_img']=$array[0];dump($post['img']);
        // dump($post);
        if(!$info) {// 上传错误提示错误信息
           // $this->error($upload->getError());
        }else{//上传成功 获取上传文件信息
            foreach($info as $file){
                $banner=D('teacher');
                if($banner->add($post)) {
                    //dump($post);
                  $this->success('上传成功','index');
                }
            }
        }

        $this->display();

    }
 public function edit($id){
     $teacher=M('teacher');
     $teachers=$teacher->find($id);
     $upload = new \Think\Upload();// 实例化上传类
     $upload->maxSize   =     3145728 ;// 设置附件上传大小
     $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
     $upload->rootPath  =      './Public/'; // 设置附件上传根目录
     $upload->savePath  =      '/Uploads/'; // 设置附件上传（子）目录
     $info   =   $upload->upload();
     $array = array();
     //原图片路径
     $imgfile = ROOT_PATH . 'Public' . $teachers['teacher_img'];
     //dump($imgfile);
     if ($info){
         //删除原图片
         if(is_file($imgfile)){
             unlink($imgfile);
         }
         //新图片路径
         foreach ($info as $val) {
             $array[] = $val['savepath'] . $val['savename'];
         }
         $post = I('post.');
         $post['teacher_img']=$array[0];
         if($teacher->create() && !empty($post['teacher_img'])){
             if($teacher->save($post)){
                  $this->success('编辑成功',U('teacher/index'));
             }else{
                  $this->error('编辑失败');
             }
         }else{
             //$this->error($teacher->getError());
         }
     }
     $this->assign('teachers',$teachers);
     $this->display();
 }

    public  function del($id){
        $teacher= M('teacher');
        $teachers = $teacher->find($id);
        $imgfile = ROOT_PATH . './public' .$teachers['image'];
        if(is_file($imgfile)){
            unlink($imgfile);
        }
        $re = $teacher->delete($id);
        if($re){
            $this->success('删除成功！' , U('teacher/index'));
        }else{
            $this->error('删除失败');
        }
    }
}
